package com.cgbl.cg.adapter

import android.os.Bundle
import com.cgbl.cg.R
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.robot.base.configs.AppConstants
import com.robot.base.model.WorksBean
import com.robot.base.util.arouter.ARouterUtil
import com.robot.base.utils.pic.ImageLoaderUtils
import com.youth.banner.Banner
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.RectangleIndicator
import com.youth.banner.listener.OnPageChangeListener

class DiscoveryWorkAdapter :
    BaseQuickAdapter<WorksBean, BaseViewHolder>(R.layout.app_item_discovery_work) {
    override fun convert(holder: BaseViewHolder, item: WorksBean) {
        holder.setText(R.id.item_tv_name, item.nickName)
            .setText(R.id.item_tv_time, item.crateDate)
            .setText(R.id.item_tv_content, item.workName)
            .setText(R.id.item_tv_like, item.likeCount ?: "0")
            .setText(R.id.item_tv_share, item.shareCount ?: "0")
            .setText(R.id.item_tv_comentCount, item.comentCount ?: "0")
        val banner = holder.getView<Banner<String, BannerImageAdapter<String>>>(R.id.item_banner)
        val list = item.images?.map { it.filePath }
        banner.setAdapter(object : BannerImageAdapter<String>(list) {
            override fun onBindView(
                holder: BannerImageHolder?,
                data: String?,
                position: Int,
                size: Int
            ) {
                ImageLoaderUtils.getGlideManager()
                    .load(data)
                    .into(holder?.imageView!!)
            }
        })
        val indicator = holder.getView<RectangleIndicator>(R.id.indicator)
        banner.setIndicator(indicator, false)
        banner.addOnPageChangeListener(object : OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {

            }

            override fun onPageSelected(position: Int) {
                holder.setText(R.id.item_tv_img_size, "${position + 1}/${item.images?.size}")
            }

            override fun onPageScrollStateChanged(state: Int) {

            }

        })
        ImageLoaderUtils.displayCirclePortrait(
            context, holder.getView(R.id.item_iv_avatar), item.head
        )
        holder.itemView.setOnClickListener {
            ARouterUtil.goToActivityWithBundle(AppConstants.Router.WORK.A_WORK_DETAIL,
                Bundle().apply {
                    putString("id", item.id)
                }
            )
        }
    }
}